package com.framework.service.common.impl;

import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.common.util.redis.RedisKeyUtil;
import com.framework.mapper.common.ProtocolMapper;
import com.framework.model.common.vo.ProtocolVo;
import com.framework.model.system.vo.SystemUserVo;
import com.framework.service.base.BaseService;
import com.framework.service.common.ProtocolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.common.impl.ProtocolServiceImpl
 * @description 协议业务接口实现类
 * @datetime 2024-06-19 14:54:56
 */
@Service("protocolServiceImpl")
public class ProtocolServiceImpl extends BaseService implements ProtocolService {
    private Logger log = LoggerFactory.getLogger(ProtocolServiceImpl.class);
    @Autowired
    private ProtocolMapper protocolMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.common.vo.ProtocolVo
     * @title 根据编号查询协议
     * @description 根据编号查询协议
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    public ProtocolVo getByIdParam(Long id) {
        ProtocolVo row = new ProtocolVo();
        row.setId(id);
        SystemUserVo systemUserVo = getUser();
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        ProtocolVo p = protocolMapper.selectByParam(row);
        if (p == null) {
            return new ProtocolVo();
        }
        return p;
    }

    /**
     * @param row 1 协议类
     * @return com.framework.common.model.response.R
     * @title 新增协议
     * @description 新增协议
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(ProtocolVo row) {
        SystemUserVo systemUserVo = getUser();
        int isExist = protocolMapper.isExist(row);
        if (isExist > NumeralUtil.POSITIVE_ZERO) {
            return R.fail("已存在相同协议!");
        }
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setCreateId(userId);
        row.setCreateTime(date);
        row.setOperaterId(userId);
        row.setOperaterTime(date);
        row.setOperaterStatus(NumeralUtil.POSITIVE_ONE);
        protocolMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param row 1 协议类
     * @return com.framework.common.model.response.R
     * @title 编辑协议
     * @description 编辑协议
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(ProtocolVo row) {
        SystemUserVo systemUserVo = getUser();
        int isExist = protocolMapper.isExist(row);
        if (isExist > NumeralUtil.POSITIVE_ZERO) {
            return R.fail("已存在相同协议!");
        }
        ProtocolVo queryParam = new ProtocolVo();
        queryParam.setId(row.getId());
        queryParam.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ZERO);
        queryParam = protocolMapper.selectByParam(queryParam);
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        if (queryParam.getInitId() == null || queryParam.getInitId().longValue() == NumeralUtil.POSITIVE_ZERO) {
            row.setInitId(queryParam.getId());
        } else {
            row.setInitId(queryParam.getInitId());
        }
        row.setId(null);
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setCreateId(userId);
        row.setCreateTime(date);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        ProtocolVo deleteRow = new ProtocolVo();
        deleteRow.setId(queryParam.getId());
        deleteRow.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        deleteRow.setOperaterTime(date);
        deleteRow.setOperaterId(userId);
        protocolMapper.delete(deleteRow);
        protocolMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除协议
     * @description 删除协议
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        ProtocolVo row = new ProtocolVo();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setId(id);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        protocolMapper.delete(row);
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除协议
     * @description 批量删除协议
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        ProtocolVo row = new ProtocolVo();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setIdList(idList);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        //protocolMapper.deleteList(row);
        return R.ok();
    }

    /**
     * @param row 1 协议类
     * @return java.util.List<com.framework.model.common.vo.ProtocolVo>
     * @title 查询协议集合
     * @description 查询协议集合
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    public List<ProtocolVo> findByList(ProtocolVo row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        List<ProtocolVo> list = protocolMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 协议类
     * @return com.framework.common.model.response.R
     * @title 分页查询协议列表
     * @description 分页查询协议列表
     * @author 龘鵺
     * @datetime 2024-06-19 14:54:56
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(ProtocolVo row) {
        SystemUserVo systemUserVo = getUser();
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        int count = protocolMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<ProtocolVo> list = protocolMapper.findPageList(row);
        return R.ok(list, count);
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 启用|禁用，角色
     * @description 启用|禁用，角色
     * @author 龘鵺
     * @datetime 2024-06-19 09:34:28
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> enable(Long id) {
        String timeKey = RedisKeyUtil.getObjKey("PROTOCOL_ENABLE_KEY", "PROTOCOL_ENABLE_KEY", id.toString());
        long expireNum = redisUtil.getExpire(timeKey);
        if (expireNum > NumeralUtil.POSITIVE_ZERO) {
            return R.fail("操作频繁,请" + expireNum + "秒后重试!");
        }
        ProtocolVo row = new ProtocolVo();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        row.setId(id);
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ZERO);
        row = protocolMapper.selectByRow(row);
        if (row == null) {
            return R.fail("信息不存在!");
        }
        redisUtil.setString(timeKey, NumeralUtil.STRING_POSITIVE_ZERO, NumeralUtil.POSITIVE_THIRTY);
        Date date = new Date();
        ProtocolVo updateRow = new ProtocolVo();
        updateRow.setId(id);
        updateRow.setOperaterId(userId);
        updateRow.setOperaterTime(date);
        updateRow.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        updateRow.setEnableStatus(row.getEnableStatus().intValue() == NumeralUtil.POSITIVE_TWO ? NumeralUtil.POSITIVE_ONE : NumeralUtil.POSITIVE_TWO);
        protocolMapper.updateByPrimaryKeySelective(updateRow);
        return R.ok();
    }
}